#ifndef __LOGGER_H__
#define __LOGGER_H__

#include<fstream>//文件流
#include"public.h"
#include"message.h"
#include<iostream>
//单例模式
//把构造函数私有化
//提供生成对象的静态方法
class Logger
{
private:
    Logger();
    bool m_LogToTerminal;//输出到终端
    bool m_LogToFile;//输出到文件
    std::fstream m_logFile;//把数据输入到文件
    LOG_LEVEL m_level;
public:
    static Logger*GetInstance()
    {
        static Logger Logger;
        return &Logger;
    }

    void SetLogToTerminal(bool logToTerminal);
    bool GetLogToTerminal()const;

    void SetLogToLogFile(bool logToFile);
    bool GetLogToLogFile()const;

    void SetLogFile(const std::string&logFileName="xsy.log");

    void SetLogLevel(LOG_LEVEL level);
    LOG_LEVEL GetLogLevel()const;
    void operator+=(const Message& msg)
    {
        if(m_LogToTerminal)
        {
            std::cout<<msg.ToString(true)<<std::endl;
        }
        if(m_LogToFile)
        {
            SetLogFile();
            m_logFile<<msg.ToString(false)<<std::endl;
        }
        if(msg.ToString().find("FATAL")!=std::string ::npos)
        {
            std::cout<<"fatal happended!"<<std::endl;
            exit(-1);
        }
    }

};

#endif
 


#define LOG(level)\
    if(level>=Logger::GetInstance()->GetLogLevel())\
    *Logger::GetInstance()+=Message(level,__FILE__,__FUNCTION__,__LINE__)\

#define LOG_INFO LOG(INFO)
#define LOG_ERROR LOG(ERROR)
#define LOG_DEBUG LOG(DEBUG)
#define LOG_FATAL LOG(FATAL)

/*
int main()
{
    LOG_INFO<<"hello world!";
    等价于
    if(INFO>=Logger::GetInstance()->GetLogLevel())
    *Logger::GetInstance()+=Message(INFO,__FILE__,__FUNCTION__,__LINE__) << "hello world!";

    return 0;
}
*/